<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button type="primary" @click="handleAdd">添加词库</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="685">
      <el-table-column label="ID序号" prop="id" width="50px" align="center"></el-table-column>
      <el-table-column label="内容" show-overflow-tooltip prop="content" width="500px" align="center"></el-table-column>
      <el-table-column label="操作人" prop="update_user" align="center" width="100px"></el-table-column>
      <el-table-column align="center" label="操作" width="120px">
        <template slot-scope="{row}">
          <el-button type="text" size="small" @click="handleUnban(row)">删除</el-button>
          <!-- <el-button type="text" size="small" @click="handleEdit(row)">编辑</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="80px" label-position="left">
        <el-form-item label="内容" prop="content">
          <el-input ref="content" v-model="dataForm.content" class="defaut-input" placeholder="填写内容" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import checkPermission from '@/utils/permission' // 权限判断函数
import waves from '@/directive/waves' // 实现点击波浪纹
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { clearChatNumbWord, handleNumbWord, getNumbWord } from '@/api/chat'
const defaultItem = {
  content: '',
}
export default {
  name: 'MonitorLog',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      permissionRoles: this.$store.state.user.permissionRoles,
      list: [],
      rules: {
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ]
      },
      total: 0,
      dataForm: Object.assign({}, defaultItem),
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 200
      },
      dialogVisible: false,
      dialogType: 'new',
      textMap: {
        edit: '编辑',
        new: '新增'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    checkPermission,
    handleAdd() {
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getNumbWord(query)
      this.listLoading = false
      this.list = res.data.items
      this.total = res.data.total
    },
    handleFilter() {
      this.getList()
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          // const isEdit = this.dialogType === 'edit'
          handleNumbWord(this.dataForm).then(response => {
            this.getList()
          })
          this.dialogVisible = false
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          return
        }
      })
    },
    handleUnban(row) {
      this.$confirm('你确定要删除该无感词吗？', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await clearChatNumbWord(row)
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(err => { console.error(err) })
    }
  }
}
</script>
